#!/bin/bash
# GenISO script by Ettore Di Giacinto <mudler@sabayonlinux.org>,
#                  Daniele Rondina <geaaru@sabayonlinux.org>
# License: MIT
# See also (and inspired by):
# https://github.com/Tomas-M/linux-live.git
# http://minimal.linux-bg.org/#home

set -e

. "/usr/share/luet-extension/geniso/func.sh"

export IMAGE_NAME="${IMAGE_NAME:-luet_os.iso}"
export LUET_PACKAGES="${LUET_PACKAGES:-}"
export LUET_REPOS="${LUET_REPOS:-}"
export LUET_INITRAMFS_REPOS="${LUET_INITRAMFS_REPOS:-}"
export LUET_BIN="${LUET_BIN:-../luet}"
export ROOT_DIR="${ROOT_DIR:-$PWD}"
export LUET_CONFIG="${LUET_CONFIG:-../conf/luet-local.yaml}"
export WORKDIR="$ROOT_DIR/isowork"
export OVERLAY="${OVERLAY:-false}"
export ISOLABEL="${ISOLABEL:-ISOIMAGE}"
export ARCH="${ARCH:-x86_64}"
export ISOIMAGE_PACKAGES="${ISOIMAGE_PACKAGES:-live/syslinux system/sabayon-live-boot}"
export UEFI_PACKAGES="${UEFI_PACKAGES:-live/systemd-boot system/sabayon-live-boot}"
export LUET_YES=true
#export BOOT_DIR="$WORKDIR/boot"
export ROOTFS_DIR="$WORKDIR/rootfs"
export OVERLAY_DIR="$WORKDIR/overlay"
export ISOIMAGE="$WORKDIR/isoimage"
export KERNEL_INSTALLED=$WORKDIR/kernel/kernel_installed
export FIRST_STAGE="${FIRST_STAGE:-}"
export GEN_ROOTFS="${GEN_ROOTFS:-true}"
export INITRAMFS_KERNEL="${INITRAMFS_KERNEL:-bzImage}"
export INITRAMFS_ROOTFS="${INITRAMFS_ROOTFS:-}"
export SCRIPT_DIR="${SCRIPT_DIR:-/usr/share/luet-extension/geniso}"
export SUDO=${SUDO:-}
export KEEP_LUET_DB=${KEEP_LUET_DB:-false}
export CA_CERTIFICATES=${CA_CERTIFICATES:-/etc/ssl/certs/ca-certificates.crt}

export LUET_GENISO_OUTPUT="${LUET_GENISO_OUTPUT:-$WORKDIR/luet_install.log}"
export LUET_BOX_INSTALLER=${LUET_BOX_INSTALLER:-0}
export LUET_OPTS="${LUET_OPTS:-}"

trap cleanup 1 2 3 6 EXIT

cleanup()
{
   umount_rootfs  "$ROOTFS_DIR"
   umount_rootfs  "$OVERLAY_DIR"
}

run_hooks()
{
  if [ -n "${OVERLAY_HOOKS}" ] ; then
    for script in ${OVERLAY_HOOKS}; do
      run_hook "${OVERLAY_DIR}" "${script}"
    done
  fi
}

cleanup

if [[ "$GEN_ROOTFS" == true ]]; then
  mkdir -p $WORKDIR

  rm -rf "$ROOTFS_DIR"
  mkdir -p "$ROOTFS_DIR"

  rm -rf "$OVERLAY_DIR"
  mkdir -p "$OVERLAY_DIR"

  if [[ "$OVERLAY" == true ]]; then
    info "Building overlay"
    if [[ -n "$FIRST_STAGE" ]]; then
      info "Installing packages for initramfs"
      if [ ${LUET_BOX_INSTALLER} -eq 1 ] ; then
        luet_box_install "$ROOTFS_DIR" "${FIRST_STAGE}" "${LUET_INITRAMFS_REPOS}"
      else
        luet_install "$ROOTFS_DIR" "${FIRST_STAGE}" "${LUET_INITRAMFS_REPOS}"
      fi
    fi

    if [ ${LUET_BOX_INSTALLER} -eq 1 ] ; then
      luet_box_install "$OVERLAY_DIR" "${LUET_PACKAGES}" "${LUET_REPOS}" "${KEEP_LUET_DB}"
    else
      luet_install "$OVERLAY_DIR" "${LUET_PACKAGES}" "${LUET_REPOS}" "${KEEP_LUET_DB}"
    fi

    run_hooks
  else
    if [ ${LUET_BOX_INSTALLER} -eq 1 ] ; then
      luet_box_install "$ROOTFS_DIR" "${LUET_PACKAGES}" "${LUET_REPOS}"
    else
      luet_install "$ROOTFS_DIR" "${LUET_PACKAGES}" "${LUET_REPOS}"
    fi
  fi
fi

for script in $(ls ${SCRIPT_DIR} | grep '^[0-9]*_.*.sh'); do
  #info "Running phase '$script'."
  ${SCRIPT_DIR}/$script || {
    err "${script}: Something went wrong! Exiting."
    exit 1
  }
done
